package com.hskn.hss.module.tloperationitem.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.tloperationitem.entity.TlOperationItem;
import com.hskn.hss.module.tloperationitem.mapper.TlOperationItemMapper;
import com.hskn.hss.module.tloperationitem.service.ITlOperationItemService;
import com.hskn.hss.utils.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 作业类型小类 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-29
 */
@Service
public class TlOperationItemServiceImpl extends ServiceImpl<TlOperationItemMapper, TlOperationItem> implements ITlOperationItemService {

    @Resource
    TlOperationItemMapper tlOperationItemMapper;

    @Override
    public AjaxResult insert(JSONObject params) throws HssException {

        TlOperationItem tlOperationItem = params.getObject("data", TlOperationItem.class);

        if (tlOperationItem.getStatus() == null || tlOperationItem.getStatus().isEmpty()) {
            tlOperationItem.setStatus("1");
        }
        tlOperationItemMapper.insert(tlOperationItem);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult update(JSONObject params) throws HssException {

        TlOperationItem tlOperationItem = params.getObject("data", TlOperationItem.class);
        tlOperationItemMapper.updateById(tlOperationItem);
        return AjaxResult.success("操作成功");
    }

    @Override
    public AjaxResult deletebyid(JSONObject params) throws HssException {
        String id = params.getString("id");
        tlOperationItemMapper.deleteById(id);
        return AjaxResult.success("操作成功");
    }

    @Override
    public TlOperationItem getDetail(JSONObject params) throws HssException {
        String id = params.getString("id");
        TlOperationItem tlOperationItem = tlOperationItemMapper.selectById(id);
        return tlOperationItem;
    }

    @Override
    public List getSelectList(JSONObject params) {
        Map<String, Object> map = new HashMap();
        map.put("opeid", params.getString("opeid"));
        List<Map> resultList = tlOperationItemMapper.getSelectList(map);
        return resultList;
    }

    @Override
    public JSONObject getList(JSONObject params) throws HssException {
        Long page = StringUtils.isEmpty(params.getLong("page")) ? 1 : params.getLong("page");
        Long size = StringUtils.isEmpty(params.getLong("size")) ? 10 : params.getLong("size");
        JSONObject json = new JSONObject();

        Map<String, Object> map = new HashMap();
        map.put("opeid", params.getString("opeid"));
        map.put("itemtype", params.getString("itemtype"));
        map.put("status", params.getString("status"));
        map.put("page", params.getString("page"));
        map.put("size", params.getString("size"));
        Page<TlOperationItem> listPage = new Page<>(page, size, true);
        IPage<TlOperationItem> iPage = tlOperationItemMapper.listTlOperationItem(listPage, map);
        json.put("total", iPage.getTotal());
        json.put("size", iPage.getSize());
        json.put("pages", iPage.getPages());
        json.put("current", iPage.getCurrent());
        json.put("resultlist", iPage.getRecords());
        return json;
    }
}
